package com.zhongke.cotrun.controller;

import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.mapper.CompanyInfoMapper;
import com.zhongke.cotrun.model.exception.MyException;
import com.zhongke.cotrun.model.pojo.CompanyInfoVo;
import com.zhongke.cotrun.model.pojo.LevelTwoComment;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.service.LevelTwoCommentService;
import com.zhongke.cotrun.shiro.sys.shiro.ShiroUtils;
import com.zhongke.cotrun.shiro.web.entity.WebUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;

@RequestMapping("/LevelTwoComment")
@RestController
public class LevelTwoCommentController {
    @Autowired
    LevelTwoCommentService levelTwoCommentService;
    @Autowired
    CompanyInfoMapper companyInfoMapper;

    /**
     * 新增二级评论
     *
     * @param levelTwoComment
     * @return
     */
    @PostMapping("/insertLevelTwoComment")
    public APIResponse insertLevelTwoComment(LevelTwoComment levelTwoComment) throws MyException {
        WebUserEntity userEntity = ShiroUtils.getWebUser();//用户信息
        if (userEntity == null) {
            throw new MyException(CodeMsg.USER_LOGIN_ERROR);
        }

        levelTwoComment.setLinkUserId(userEntity.getUserId().intValue());
        if("1".equals(userEntity.getType())){
            levelTwoComment.setMyName(userEntity.getRealName());
            levelTwoComment.setMyHead(userEntity.getAccountHead());
        }else {
            CompanyInfoVo c =companyInfoMapper.selectCompanyInfoByUserId(userEntity.getUserId().intValue());
            levelTwoComment.setMyName(c.getCompanyName());
            levelTwoComment.setMyHead(c.getCompanyLogo());
        }






        LevelTwoComment levelTwoComment1 = levelTwoCommentService.insertLevelTwoComment(levelTwoComment);
        if (levelTwoComment1 != null) {
            return APIResponse.returnSuccess(levelTwoComment1);
        }
        return APIResponse.returnFail(CodeMsg.ADD_ERROR);
    }

    /**
     * 根据id查看二级评论
     *
     * @param id
     * @return
     */
    @GetMapping("/selectLevelTwoCommentById")
    public APIResponse selectLevelTwoCommentById(BigInteger id,String type) {

        return APIResponse.returnSuccess(levelTwoCommentService.selectLevelTwoCommentById(id,type));


    }

    /**
     * 删除二级评论（状态）
     *
     * @param LevelTwoCommentId
     * @return
     */
    @GetMapping("/delectLevelTwoCommentById")
    public APIResponse delectLevelTwoCommentById(Integer LevelTwoCommentId) throws MyException {
        WebUserEntity userEntity = ShiroUtils.getWebUser();//用户信息
        if (userEntity == null) {
            throw new MyException(CodeMsg.USER_LOGIN_ERROR);
        }

        if (levelTwoCommentService.updateStateLevelTwocomment(1, LevelTwoCommentId, userEntity.getUserId().intValue())) {
            return APIResponse.returnSuccess();
        }
        return APIResponse.returnFail(CodeMsg.UPDATE_ERROR);

    }

}
